import { Router } from 'express';
import C from '../app/constants/constants.color';
import { v4 } from 'uuid';

const router = Router();

const dispatchAndRespond = (req, res, action) => {
  req.store.dispatch(action);
  res.status(200).json(action);
};

router.get("/colors", (req, res) =>
  res.status(200).json(req.store.getState().colors)
);

router.post("/colors", (req, res) =>
  dispatchAndRespond(req, res, {
    type: C.ADD_COLOR,
    id: v4(),
    title: req.body.title,
    color: req.body.color,
    timestamp: new Date().toString()
  })
);

router.put("/color/:id", (req, res) =>
  dispatchAndRespond(req, res, {
    type: C.RATE_COLOR,
    id: req.params.id,
    rating: parseInt(req.body.rating)
  })
);

router.delete("/color/:id", (req, res) =>
  dispatchAndRespond(req, res, {
    type: C.REMOVE_COLOR,
    id: req.params.id
  })
);

export default router;
